package com.woniuxy.ptapp.common.util;
import cn.hutool.json.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.nimbusds.jose.JWSObject;
import com.woniuxy.commons.jwt.UserConst;
import com.woniuxy.commons.util.JwtUtil;
import com.woniuxy.commons.util.TokenToUserDtoUtil;
import com.woniuxy.commons.util.UserDto;
import com.woniuxy.commons.util.WebUtil;
import com.woniuxy.ptapp.common.param.LoginUser;
import io.jsonwebtoken.Claims;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.text.ParseException;
import java.util.List;

@Component
public class GetLogingUser {
    @Resource
    JwtUtil jwtUtil;

    public LoginUser getLogingUser(){
        System.out.println("getLoginUserSuccess................");
        String token = WebUtil.getRequest().getHeader("Authorization");
        String realtoken = token.replace("Bearer " , "");
        JWSObject jwsObject = null;
        try {
            jwsObject = JWSObject.parse(realtoken);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String userStr = jwsObject.getPayload().toString();
        JSONObject userJsonObject = new JSONObject(userStr);
        System.out.println(userStr);
        String loginName =(String)userJsonObject.get("LoginName");
        Integer LoginId =(Integer)userJsonObject.get("LoginId");
        List<String> LoginRole =(List<String>)userJsonObject.get("LoginRole");
        LoginUser loginUser = new LoginUser();
        loginUser.setLoginId(LoginId);
        loginUser.setLoginRole(Integer.parseInt(LoginRole.get(0)));
        loginUser.setLoginName(loginName);
        return loginUser;
    }
}
